package br.com.lambdacore.ui.stripes.actionresolver;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.controller.NameBasedActionResolver;

public class ActionResolver extends NameBasedActionResolver {

	@Override
	protected String getBindingSuffix() {
		return "";
	}

	@Override
	protected List<String> getFindViewAttempts(String url) {
		System.out.println("url: " + url);
		List<String> defaultViews = super.getFindViewAttempts(url);
		List<String> customViews = new ArrayList<String>(defaultViews.size());
		for (String view : defaultViews) {
			customViews.add("/WEB-INF/jsp" + view);
		}
		System.out.println("customViews:" + customViews);
		return customViews;
	}

	@Override
	protected String getUrlBinding(String actionBeanName) {
		String result = super.getUrlBinding(actionBeanName);
		result = this.convertToLowerCaseWithUnderscores(result);
		return result;
	}

	private String convertToLowerCaseWithUnderscores(String string) {
		System.out.println(string);
		StringBuilder builder = new StringBuilder();
		for (int i = 0, t = string.length(); i < t; i++) {
			char ch = string.charAt(i);
			if (Character.isUpperCase(ch)) {
				ch = Character.toLowerCase(ch);
				if (i > 1) {
					builder.append('_');
				}
			}
			builder.append(ch);
		}
		return builder.toString();
	}
}
